package com.cgw.game.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * @类描述 色块进度条
 * @作者 YF
 * @创建时间 2020/4/17 11:45
 */
public class ScoreBlockProgress extends View {
    /**
     * 背景画笔
     * */
    private Paint mBgPaint;

    /**
     * 三角画笔
     * */
    private Paint mPaint;
    /**
     * 宽
     * */
    private int width;
    /**
     * 高
     * */
    private int height;


    /**
     * 上下文对象
     * */
    private Context context;

    /**
     * 左右默认边距
     * */
    private float defaultMargin = 20f;

    /**
     * 上下默认边距
     * */
    private float tobAndBottomMargin = 10f;
    private float padding = 8f;

    private float progress = 100f;
    private float max = 100;


    int[] colors = new int[]{
            Color.parseColor("#f95d67"),
            Color.parseColor("#f98468"),
            Color.parseColor("#fb9665"),
            Color.parseColor("#fda358"),
            Color.parseColor("#ffb652"),
            Color.parseColor("#ffd762"),
            Color.parseColor("#f7ee73"),
            Color.parseColor("#93eec4"),
            Color.parseColor("#6ac3e5"),
            Color.parseColor("#508cfa"),
    };

    public ScoreBlockProgress(Context context) {
        this(context,null);
    }

    public ScoreBlockProgress(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ScoreBlockProgress(Context context, @Nullable AttributeSet attrs, int defStyleAttr){
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }

    private void init() {
        mBgPaint = new Paint();
        mBgPaint.setAntiAlias(true);

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.parseColor("#3e3e3e"));
        mPaint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        width = measureSize(widthMeasureSpec);
        height = measureSize(heightMeasureSpec);
        setMeasuredDimension(width,height);
    }
    private int measureSize(int measureSpec) {
        int length;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);
        if(mode == MeasureSpec.EXACTLY){
            length = size;
        }else{
            length = DensityUtil.dip2px(context,40);
            if(mode == MeasureSpec.AT_MOST){
                length = Math.min(length,size);
            }
        }
        return length;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        drawBackGround(canvas);
        drawProgress(canvas);
    }


    //画背景
    private void drawBackGround(Canvas canvas){
        mBgPaint.setColor(Color.WHITE);
        canvas.drawRoundRect(defaultMargin,tobAndBottomMargin,width-defaultMargin,height-tobAndBottomMargin,15f,15f,mBgPaint);

        float rectWidth = width-2*(defaultMargin+padding) - colors.length-1; //中间的间隔等于色块个数减1
        float space = rectWidth/colors.length;
        for (int i = 0; i < colors.length; i++) {
            mBgPaint.setColor(colors[i]);
            if(i==0){
                canvas.drawRect(defaultMargin+padding+(space*i)+15f,tobAndBottomMargin+padding,defaultMargin+padding+(space*i)+space,height-tobAndBottomMargin-padding,mBgPaint);
                canvas.drawRoundRect(defaultMargin+padding+(space*i),tobAndBottomMargin+padding,defaultMargin+padding+(space*i)+space,height-tobAndBottomMargin-padding,15f,15f,mBgPaint);
            }else if(i==colors.length-1){
                canvas.drawRect(defaultMargin+padding+(space*i)+i,tobAndBottomMargin+padding,defaultMargin+padding+(space*i)+i+space-15f,height-tobAndBottomMargin-padding,mBgPaint);
                canvas.drawRoundRect(defaultMargin+padding+(space*i)+i,tobAndBottomMargin+padding,defaultMargin+padding+(space*i)+i+space,height-tobAndBottomMargin-padding,15f,15f,mBgPaint);
            }else {
                canvas.drawRect(defaultMargin+padding+(space*i)+i,tobAndBottomMargin+padding,defaultMargin+padding+(space*i)+i+space,height-tobAndBottomMargin-padding,mBgPaint);
            }
        }
    }

    //画进度
    private void drawProgress(Canvas canvas){
        float rectWidth = width-2*(defaultMargin+padding) - colors.length-1; //中间的间隔等于色块个数减1
        float step = rectWidth / max;
        //画三角点
        Path path = new Path();
        path.moveTo((step*progress)+defaultMargin+padding,40);
        path.lineTo((step*progress)+defaultMargin-20+padding,0);
        path.lineTo((step*progress)+defaultMargin+20+padding,0);
        path.close();
        canvas.drawPath(path,mPaint);

    }


    public void setProgress(float progress) {
        startAnimator(progress);
    }

    public void setMax(float max) {
        this.max = max;
    }
    ValueAnimator animator;
    private void startAnimator(float pro){
        if(animator!=null&&animator.isRunning()){
            animator.cancel();
            animator = null;
        }
        animator = ValueAnimator.ofFloat(pro);
        animator.setDuration(1500);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                progress = (float)animation.getAnimatedValue();
                invalidate();
            }
        });
        animator.start();
    }
}
